//
//  DHProductListController.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/13.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHProductListController.h"
#import "DHBrandProductCollectionCell.h"
#import "DHS2_ProductDetailController.h"

@interface DHProductListController ()

@property (assign, nonatomic) NSInteger pageIndex;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic) NSMutableArray *dataArray;

@end

@implementation DHProductListController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.titleLabel.text = self.navTitle;
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"DHBrandProductCollectionCell" bundle:nil] forCellWithReuseIdentifier:kDHBrandProductCollectionCell];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.mj_header = [DHMJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadProduct)];
    self.collectionView.mj_footer = [DHMJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreProduct)];
    
    [self loadProduct];
}

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)loadProduct{
    self.pageIndex = 0;
    [self.collectionView.mj_footer resetNoMoreData];
    [[DHNetAPIClient shareDHNetAPIClient] request_productsWithParams:@{self.paramsKey:self._id,@"pageIndex":@(self.pageIndex),@"pageSize":@(20)} andBlock:^(id data, NSError *error) {
        [self.collectionView.mj_header endRefreshing];
        [self.dataArray removeAllObjects];
        [self.dataArray addObjectsFromArray:[data objectForKey:@"products"]];
        if (self.dataArray.count < 20) {
            [self.collectionView.mj_footer endRefreshingWithNoMoreData];
        }
        self.pageIndex += 1;
        [self.collectionView reloadData];
    }];
}

- (void)loadMoreProduct{
    [[DHNetAPIClient shareDHNetAPIClient] request_productsWithParams:@{self.paramsKey:self._id,@"pageIndex":@(self.pageIndex),@"pageSize":@(20)} andBlock:^(id data, NSError *error) {
        [self.collectionView.mj_footer endRefreshing];
        [self.dataArray addObjectsFromArray:[data objectForKey:@"products"]];
        if (self.dataArray.count < 20 * (self.pageIndex + 1)) {
            [self.collectionView.mj_footer endRefreshingWithNoMoreData];
        }
        self.pageIndex += 1;
        [self.collectionView reloadData];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UICollectionViewDataSource and UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    DHBrandProductCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kDHBrandProductCollectionCell forIndexPath:indexPath];
    [cell uploadCell:self.dataArray[indexPath.row]];
    return cell;
}

#pragma mark ---- UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat w = (kScreenWidth - 5*4) / 3;
    CGFloat h = w * 1.29 + 66;
    return (CGSize){w,h};
}

// 两行之间的最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

// 两个cell之间的最小间距，是由API自动计算的，只有当间距小于该值时，cell会进行换行
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 5;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *model = [self.dataArray objectAtIndex:indexPath.row];
    DHS2_ProductDetailController *vc = [[DHS2_ProductDetailController alloc] initWithProduct:model];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
